package com.hr.剑指offer.work1;

import java.util.Arrays;

/**
 * @author 周恒哲
 * @date 2020/06/12
 * 二分法
 **/
public class 面试题11_旋转数组的最小数字 {
    public static int minArray(int[] numbers) {
        Arrays.sort(numbers);
        return numbers[0];
    }
    public static int minArray2(int[] numbers) {
        int left=0;
        int right=numbers.length-1;
        while (right>left){
            int mid=left+(right-left)/2;
            if (numbers[mid]>numbers[right]){
                left=mid+1;
            }else if (numbers[mid]<numbers[right]){
                right=mid;
            }else {
                right--;
            }
        }
        return numbers[left];
    }
    public static int minArray3(int[] numbers) {
        for (int i = 0; i < numbers.length-1; i++) {
            if (numbers[i]>numbers[i+1]){
                return numbers[i+1];
            }
        }
        return numbers[0];
    }

    public static void main(String[] args) {
        System.out.println(面试题11_旋转数组的最小数字.minArray(new int[]{
                3, 4, 5, 1, 2
        }));
    }
}
